﻿using AgencyManagerSystem.Common;
using AgencyManagerSystem.DataFetcher;
using AgencyManagerSystem.Model;
using System;
using System.Collections.Generic;
using System.Text;

namespace AgencyManagerSystem.Core.Utility
{
    public static class MyHttpContextHelper
    {
        public static void SetHttpContext(out string access_token)
        {
            access_token = null;
            if (MyHttpContext.Current == null)
            {
                access_token = null;
                LogHelper.Err(typeof(MyHttpContext).GetType(), "MyHttpContext.Current不能为空");
                return;
            }
            if (MyHttpContext.Current.Request.Headers.ContainsKey(Definer.ACCESS_TOKEN)
                && !MyHttpContext.Current.Request.Headers.ContainsKey(Definer.CURRENT_PROFILE))
            {
                access_token = MyHttpContext.Current.Request.Headers[Definer.ACCESS_TOKEN].ToString();
                const string c_bearer = "Bearer ";
                if (access_token.StartsWith(c_bearer))
                    access_token = access_token.Substring(c_bearer.Length, access_token.Length - c_bearer.Length);
                try
                {
                    UserProfile profile = TokenManager.RestoreToken(access_token);
                    if (profile != null&&!MyHttpContext.Current.Items.ContainsKey(Definer.CURRENT_PROFILE))
                    {
                        MyHttpContext.Current.Items.Add(Definer.CURRENT_PROFILE, ToFullUserProfile(profile));
                        LogHelper.Log(typeof(MyHttpContext).GetType(), String.Format("用户{0}正在请求资源", profile.UserId));
                    }
                }
                catch (Exception ex)
                {
                    LogHelper.Err(typeof(MyHttpContext).GetType(), String.Format("解释Token {0} 失败:{1}-{2}", access_token, ex.Message, ex.StackTrace));
                }
            }
            
        }
        public static FullUserProfile ToFullUserProfile(UserProfile userProfile)
        {
            FullUserProfile fullUserProfile = new FullUserProfile(userProfile);
            UserFetcher userFetcher = new UserFetcher();
            UserModel userModel = userFetcher.Get(userProfile.UserId);


            if (userModel != null)
            {
                fullUserProfile.Avatar = userModel.Avatar;
                fullUserProfile.Department = userModel.Department;
                fullUserProfile.UserName = userModel.Name;
                fullUserProfile.Type = (UserType)userModel.Type;
                fullUserProfile.UserCode = userModel.Code;
                if (userModel.Retail)
                {
                    RetailMemberFetcher retailMemberFetcher = new RetailMemberFetcher();
                    var retailMember = retailMemberFetcher.GetByUser(userModel.AutoId);
                    if (retailMember != null)
                        fullUserProfile.Site = retailMember.Retail;
                }
                if (userModel.Supplier)
                {
                    SupplierMemberFetcher supplierMemberFetcher = new SupplierMemberFetcher();
                    var suppMember = supplierMemberFetcher.GetByUser(userModel.AutoId);
                    if (suppMember != null)
                        fullUserProfile.Site = suppMember.Supplier;
                }
            }
            return fullUserProfile;
        }
    }
}
